home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / ShareMailGiftware / AmigaTalk / general / Pen.st < prev    next >
Text File  |  2002-10-27  |  6KB  |  233 lines

  1. "-------------------------------------------------------------"
  2. "  the following use the primitives interfacing to the plot(3)"
  3. "  routines.                                                  "
  4. "           pen - a simple drawing instrument                 "
  5. "-------------------------------------------------------------"
  6.  
  7. Class Pen :Object
  8. ! title x y angle width height fpen bpen !
  9. [
  10.    movePlotEnvBy: deltaPoint
  11.       <primitive 169 2 title (deltaPoint x) (deltaPoint y)>.
  12.       ^ self
  13. |
  14.    setLineType: bitPattern
  15.       <primitive 179 bitPattern>.
  16.       ^ self
  17. |
  18.    drawText: text at: aPoint
  19.       <primitive 178 text (aPoint x) (aPoint y) fpen bpen>.
  20.       x <- (aPoint x).
  21.       y <- (aPoint y).
  22.       ^ self
  23. |
  24.    drawBox: fromPoint to: toPoint
  25.       <primitive 175 (fromPoint x) (fromPoint y) (toPoint x) (toPoint y)>.
  26.       x <- (toPoint x).
  27.       y <- (toPoint y).
  28.       ^ self
  29. |
  30.    drawCircleAt: aPoint radius: r
  31.       <primitive 174 (aPoint x) (aPoint y) r>.
  32.       x <- (aPoint x).
  33.       y <- (aPoint y).        "Leave us at the center of the circle."
  34.       ^ self
  35. |
  36.    circleRadius: rad         "Draw a circle centered at current location."
  37.       <primitive 174 x y rad>.
  38.       ^ self
  39. |
  40.    drawTo: aPoint
  41.       <primitive 172 (aPoint x) (aPoint y)>.
  42.       x <- (aPoint x).
  43.       y <- (aPoint y).
  44.       ^ self
  45. |
  46.    goTo: aPoint
  47.       <primitive 171 (aPoint x) (aPoint y)>.
  48.       x <- aPoint x.
  49.       y <- aPoint y.
  50.       ^ self
  51. |
  52.    drawLine: fromPoint to: toPoint
  53.       <primitive 177 (fromPoint x) (fromPoint y) (toPoint x) (toPoint y)>.
  54.       x <- (toPoint x).
  55.       y <- (toPoint y).
  56.       ^ self
  57. |
  58.    drawPoint: aPoint
  59.       <primitive 173 (aPoint x) (aPoint y)>.
  60.       x <- (aPoint x).
  61.       y <- (aPoint y).
  62.       ^ self
  63. |
  64.    direction             "Which way are we going?" 
  65.       ^ angle
  66. |
  67.    direction: radians    "Set the direction to go."
  68.       angle <- radians.
  69.       ^ self
  70. |
  71.    erase                 "Blank out the Current PlotEnv Window."
  72.       <primitive 170>.
  73.       ^ self
  74. |
  75.    extent                "Tell User how large the Plot area is."
  76.       ^ width @ height
  77. |
  78.    location              "Tell User where the pen is."
  79.       ^ x @ y
  80. |
  81.    titleIs
  82.       ^ title
  83. |
  84.    center                "goTo the center of the Plot region."
  85.       self goTo: (width / 2) @ (height / 2).
  86.       ^ self
  87. |
  88.    tellPens              "Tell User which pens are being used."
  89.       ^ fpen @ bpen.
  90.       ^ self
  91. |
  92.    setPens: penSet       "penSet is of Class Point (front @ back)."
  93.       <primitive 176 (penSet x) (penSet y)>.
  94.       fpen <- (penSet x).
  95.       bpen <- (penSet y).
  96.       ^ self
  97. |
  98.    go: anAmount ! newx newy !
  99.       (angle isKindOf: Radian)
  100.         ifTrue:  [newx <- ((angle sin) * anAmount) rounded + x.
  101.                   newy <- ((angle cos) * anAmount) rounded + y
  102.                  ]
  103.         ifFalse: [newx <- (((angle radians) sin) * anAmount) rounded + x.
  104.                   newy <- (((angle radians) cos) * anAmount) rounded + y
  105.                  ].
  106.  
  107.       self drawTo: newx @ newy.  "go: leaves drawn pixels as it moves."
  108.       ^ self
  109. |
  110.    turn: addedRadians
  111.       angle <- angle + addedRadians.
  112.       ^ self
  113. |
  114.    new
  115.       title  <- 'Unknown Plot'. 
  116.       angle  <- Radian new: 0.
  117.       x      <- 160.
  118.       y      <- 100.
  119.       width  <- 320.
  120.       height <- 200.
  121.       fpen   <- 1.
  122.       bpen   <- 0.
  123.       ^ self
  124. |
  125.    new: newPlotTitle            "We want a unique Plot Title!"
  126.       (newPlotTitle isKindOf: String)
  127.          ifTrue:  [ title <- newPlotTitle  ]
  128.          ifFalse: [ title <- 'Unknown Plot'].
  129.  
  130.       angle  <- Radian new: 0.
  131.       x      <- 160.
  132.       y      <- 100.
  133.       width  <- 320.
  134.       height <- 200.
  135.       fpen   <- 1.
  136.       bpen   <- 0.
  137.       ^ self
  138. |
  139.    openPlotEnv: sizePoint ! nx ny !
  140.       nx <- sizePoint x.
  141.       ny <- sizePoint y.
  142.        
  143.       (<primitive 169 1 title nx ny> == true)
  144.          ifFalse:  [ self error: 'openPlotEnv ', title, ' did NOT open!'. 
  145.                      ^ nil
  146.                    ]
  147.          ifTrue:   [ angle  <- Radian new: 0.
  148.                      x      <- (nx / 2).
  149.                      y      <- (ny / 2).
  150.                      width  <- nx.
  151.                      height <- ny.
  152.                      ^ self
  153.                    ]
  154. |
  155.    closePlotEnv: whichPlotTitle
  156.       (<primitive 169 0 whichPlotTitle> == true)
  157.         ifFalse: [self error: 'PlotEnv ',whichPlotTitle,' did NOT close!'.
  158.                   ^ self
  159.                  ].
  160.       ^ nil
  161. ]
  162.  
  163. "----------------------------------------------------"
  164. " FormPen - a collection of lines                    "
  165. " I don't think this class will work as written (JTS)"
  166. "----------------------------------------------------"
  167.  
  168. Class FormPen :Pen
  169. ! lines !
  170. [
  171.    new
  172.       lines <- Bag new
  173. |
  174.    add: startingPoint to: endingPoint "Methinks this is broken:"
  175.       lines add: ( Point new ; x: startingPoint ; y: endingPoint ).
  176.       ^ self
  177. |
  178.    with: aPen displayAt: location ! xOffset yOffset sPoint ePoint !
  179.       xOffset <- (location x).
  180.       yOffset <- (location y).
  181.  
  182.       lines do: [:pair |
  183.  
  184.          sPoint <- (pair x).
  185.          ePoint <- (pair y).
  186.  
  187.          aPen   goTo: (sPoint x + xOffset) @ (sPoint y + yOffset).
  188.          aPen drawTo: (ePoint x + xOffset) @ (ePoint y + yOffset).
  189.          ].
  190.  
  191.       ^ self
  192. ]
  193.  
  194. "----------------------------------------------------"
  195. " SavePen - a way to save the drawings made by a pen "
  196. "----------------------------------------------------"
  197.  
  198. Class SavePen :FormPen
  199. ! saveForm !
  200. [
  201.    setForm: aForm
  202.       saveForm <- aForm.
  203.       ^ self
  204. |
  205.    goTo: aPoint
  206.       super    goTo: aPoint.
  207.       saveForm  add: (self location) to: aPoint.
  208.       super    goTo: aPoint.
  209.       ^ self
  210. ]
  211.  
  212. "-----------------------------------------------------"
  213. " ShowPen - show off some of the capabilities of pens."
  214. "-----------------------------------------------------"
  215.  
  216. Class ShowPen :Object
  217. ! bic !
  218. [
  219.    withPen: aPen "aPen has to be init'd & open before using these methods."
  220.       bic <- aPen.
  221.       ^ self
  222. |
  223.    poly: nSides length: length
  224.       nSides timesRepeat:  [ bic   go: length.
  225.                              bic turn: (6.2831853 / nSides)  "2 PI"
  226.                            ].
  227.       ^ self
  228. |
  229.    spiral: n angle: a
  230.       (1 to: n) do: [:i | bic go: i. bic turn: a].
  231.       ^ self
  232. ]
  233.